package com.pxmeta.user.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.pxmeta.common.exception.ServiceException;
import com.pxmeta.common.utils.StringUtils;
import com.pxmeta.skill.entity.Skill;
import com.pxmeta.skill.service.ISkillService;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.entity.UserTag;
import com.pxmeta.user.entity.enums.TagCategoryEnum;
import com.pxmeta.user.entity.vo.UserTagVO;
import com.pxmeta.user.mapper.UserTagMapper;
import com.pxmeta.user.service.IUserService;
import com.pxmeta.user.service.IUserTagService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户标签Service接口
 *
 * @author wcl
 * @date 2022-03-21
 */
@Service
@RequiredArgsConstructor
public class UserTagServiceImpl extends ServiceImpl<UserTagMapper, UserTag> implements IUserTagService {

    private final ISkillService skillService;

    private final IUserService userService;


    @Override
    public List<UserTagVO> getAllTagList(Long userId) {
        User user = userService.getById(userId);
        String tagIds = user.getTagIds();
        LambdaQueryWrapper<UserTag> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserTag::getCategory, TagCategoryEnum.USER_TAG.getCategory());
        List<UserTag> tags = this.list(wrapper);
        List<UserTagVO> finalTags = Lists.newLinkedList();
        List<String> userTags = Lists.newLinkedList();
        if (StringUtils.isNotBlank(tagIds)){
             userTags = Arrays.asList(tagIds.split(","));
        }
        for (UserTag tag : tags) {
            UserTagVO vo = new UserTagVO();
            BeanUtils.copyProperties(tag, vo);
            if (CollectionUtil.isNotEmpty(userTags) &&
                    userTags.contains(tag.getId().toString())){
                vo.setIsAdd(true);
            }
            finalTags.add(vo);
        }
        return finalTags;
    }

    @Override
    public void addTags(Long userId, List<Long> tagIds) {
        User user = userService.getById(userId);
        Set<String> tagIdSet = tagIds.stream().map(String::valueOf).collect(Collectors.toSet());
        if (tagIdSet.size()>10){
            throw new ServiceException("用户标签数量不能大于10个！");
        }
        String tagIdStr = StrUtil.join(",", tagIdSet);
        user.setTagIds(tagIdStr);
        userService.updateById(user);
    }

    @Override
    public List<UserTag> getEvaluateTags(Long skillId) {
        Skill skill = skillService.getById(skillId);
        LambdaQueryWrapper<UserTag> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserTag::getType, skill.getCategory());
        wrapper.eq(UserTag::getCategory,TagCategoryEnum.ORDER_EVALUATE_TAG.getCategory());

        return this.list(wrapper);
    }
}
